package interfaces

import (
	"fmt"
	"xrt/utils/file/log"
)

type IfSql interface {
	Connect(map[string]string)
	Test()
	Query(string) []map[string]string
	Execute(string) error
	Select(table string, params ...interface{}) []map[string]string
	Insert(table string, params ...interface{}) bool
	Update(table string, params ...interface{}) bool
	Delete(table string, params ...interface{}) bool
	Table(table string) OrmModel //Orm支持
}

//ORM模型
type OrmModel struct {
	field     []string
	data      []string
	where     string
	order     string
	sorts     string
	limit     []int
	sqlString string
	Table     string
	SqlConn   IfSql
}

//生成Sql语句 TODO
func (m *OrmModel) generateSql() {
}

func (m *OrmModel) Field(fieldName ...string) *OrmModel {
	for _, field := range fieldName {
		m.field = append(m.field, field)
	}
	return m
}

func (m *OrmModel) Where(condition interface{}) *OrmModel {
	switch condition.(type) {
	case string:
		m.where = condition.(string)
	case map[string]string:
		for k, v := range condition.(map[string]string) {
			m.where += fmt.Sprintf("%s = '%s'", k, v)
			m.where += " and"
		}
		m.where = m.where[:len(m.where)-4]
	default:
		log.Error("Wrong Arguments for Where.")
	}
	return m
}

func (m *OrmModel) Order(fieldSort string) *OrmModel {
	m.order = fieldSort
	return m
}

func (m *OrmModel) Limit(numb1 int, numb2 ...int) *OrmModel {
	m.limit = append(m.limit, numb1)
	if len(numb2) > 0 {
		m.limit = append(m.limit, numb2[0])
	}
	return m
}

//查询全部结果
func (m *OrmModel) All() []map[string]string {
	return nil
}

//查询一条结果
func (m *OrmModel) Get() map[string]string {
	return nil
}

//执行Insert语句
func (m *OrmModel) Add() bool {
	return true
}

//执行Delete语句
func (m *OrmModel) Del() bool {
	return true
}

//智能通用执行
func (m *OrmModel) Go() error {
	return m.SqlConn.Execute(m.sqlString)
}
